export const transferCitySql = (text: string): Promise<string> => {
  return new Promise((resolve, reject) => {
    try {
      let startId = 2;
      const data = JSON.parse(text);
      let sql = "";
      data.forEach((item: Record<string, any>) => {
        const parentId = startId;
        sql +=
          "INSERT INTO `sys_region` (`id`, `name`, `level`, `parent_id`, `code`) VALUES (" +
          startId++ +
          ", '" +
          item.name +
          "', 2, 1, " +
          item.code +
          ");\n";
        item?.city?.forEach((cItem: Record<string, any>) => {
          let aParentId = startId;
          if (cItem?.name === "市辖区") {
            aParentId--;
            sql += "";
          } else {
            sql +=
              "INSERT INTO `sys_region` (`id`, `name`, `level`, `parent_id`, `code`) VALUES (" +
              startId++ +
              ", '" +
              cItem.name +
              "', 3, " +
              parentId +
              ", " +
              cItem.code +
              ");\n";
          }

          cItem?.area?.forEach((aItem: Record<string, any>) => {
            if (aItem?.name === "市辖区") {
              sql += "";
            } else {
              sql +=
                "INSERT INTO `sys_region` (`id`, `name`, `level`, `parent_id`, `code`) VALUES (" +
                startId++ +
                ", '" +
                aItem.name +
                "', 4, " +
                aParentId +
                ", " +
                aItem.code +
                ");\n";
            }
          });
        });
      });
      resolve(sql);
    } catch (err) {
      console.error(err);
      reject("转换异常");
    }
  });
};
